<template>
  <div class>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="600px" @close="close">
      <el-form ref="form" :model="form" label-width="120px">
        <el-form-item label="排序">
          <el-input v-model.trim="form.sort" autocomplete="off" type="number"></el-input>
        </el-form-item>
        <el-form-item label="活动类型">
          <el-input v-model.trim="form.type" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="活动时间">
          <el-date-picker v-model="timeData" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 300px;"></el-date-picker>
        </el-form-item>
        <el-form-item label="活动规则">
          <el-input v-model.trim="form.details" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="medium" @click="close">取 消</el-button>
        <el-button size="medium" type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { activitySave } from '@/api/home';
import { changeDate } from '@/utils/index';
export default {
  name: 'goodlistedit',
  created() {},
  data() {
    return {
      form: {
        details: '',
        sort: '',
        type: ''
      },
      timeData: [],
      content: '',
      title: '',
      dialogFormVisible: false
    };
  },
  methods: {
    async showEdit(row) {
      if (!row) {
        this.title = '添加';
      } else {
        this.title = '编辑';
        this.form = Object.assign({}, row);
      }
      this.dialogFormVisible = true;
    },
    close() {
      this.$refs['form'].resetFields();
      this.form = this.$options.data().form;
      this.dialogFormVisible = false;
    },
    save() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          if (this.timeData) {
            let startTime = changeDate(this.timeData[0]);
            let endTime = changeDate(this.timeData[1]);
            this.form.startTime = startTime;
            this.form.endTime = endTime;
          }
          const { msg } = await activitySave(this.form);
          this.$baseMessage(msg, 'success');
          this.$emit('fetchData');
          this.close();
        } else {
          return false;
        }
      });
    }
  }
};
</script>
